package com.luyue.zagj.activity.home;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.luyue.zagj.BaseActivity;
import com.luyue.zagj.R;
import com.luyue.zagj.fragment.InsurajcePilferFragment;
import com.luyue.zagj.fragment.InsuranceDrivingFragment;
import com.luyue.zagj.fragment.InsuranceThreeFragment;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InsuranceClaimsActivity extends BaseActivity {

    @Bind(R.id.insurance_claims_cancel_btn)
    Button insuranceClaimsCancelBtn;
    @Bind(R.id.insurance_claims_top_rl)
    RelativeLayout insuranceClaimsTopRl;
    @Bind(R.id.insurance_claims_tab)
    TabLayout insuranceClaimsTab;
    @Bind(R.id.insurance_claims_vp)
    ViewPager insuranceClaimsVp;


    private List<String> mTitleList = new ArrayList<>();//页卡标题集合
    private Fragment f1, f2, f3;//页卡视图

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insurance_claims);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        mTitleList.add("盗抢险");
        mTitleList.add("架意险");
        mTitleList.add("三责险");
        insuranceClaimsTab.setTabMode(TabLayout.MODE_FIXED);//设置tab模式，当前为系统默认模式
        insuranceClaimsTab.addTab(insuranceClaimsTab.newTab().setText(mTitleList.get(0)));//添加tab选项卡
        insuranceClaimsTab.addTab(insuranceClaimsTab.newTab().setText(mTitleList.get(1)));
        insuranceClaimsTab.addTab(insuranceClaimsTab.newTab().setText(mTitleList.get(2)));

        MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager(), insuranceClaimsTab.getTabCount());
        insuranceClaimsVp.setAdapter(mAdapter);//给ViewPager设置适配器
        insuranceClaimsVp.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(insuranceClaimsTab));
        insuranceClaimsTab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                insuranceClaimsVp.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    class MyPagerAdapter extends FragmentStatePagerAdapter {
        int nNumOfTabs;

        public MyPagerAdapter(FragmentManager fm, int nNumOfTabs) {
            super(fm);
            this.nNumOfTabs = nNumOfTabs;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    InsurajcePilferFragment tab1 = new InsurajcePilferFragment();
                    return tab1;
                case 1:
                    InsuranceDrivingFragment tab2 = new InsuranceDrivingFragment();
                    return tab2;
                case 2:
                    InsuranceThreeFragment tab3 = new InsuranceThreeFragment();
                    return tab3;
            }
            return null;
        }

        @Override
        public int getCount() {
            return nNumOfTabs;
        }
    }

    @OnClick(R.id.insurance_claims_cancel_btn)
    public void onClick() {
        onBackPressed();
    }
}
